package com.talent.web.config.influxdb;

import com.influxdb.client.*;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: hszhou
 * @Date: 2025/4/3 9:34
 * @Description: 必须描述类做什么事情, 实现什么功能
 */
@Configuration
@RequiredArgsConstructor
@ConditionalOnProperty(name = "influx.enabled", havingValue = "true")
public class InfluxConfig {

    private final TlInfluxProperties influxDbProperties;





    @Bean
    public InfluxDBClient influxDBClient(){
        return InfluxDBClientFactory.create(influxDbProperties.getUrl(),
                influxDbProperties.getToken().toCharArray(),
                influxDbProperties.getOrg(),
                influxDbProperties.getBucket());
    }

    @Bean
    public WriteApi writeApi() {
        InfluxDBClient client = influxDBClient();
        WriteOptions options = WriteOptions.builder()
        .batchSize(influxDbProperties.getBatchSize())
        .flushInterval(influxDbProperties.getFlushInterval())
       .build();
       return client.makeWriteApi(options);
    }

    @Bean
    public QueryApi queryApi(){
        InfluxDBClient client = influxDBClient();
        return client.getQueryApi();
    }

    @Bean
    public WriteApiBlocking writeApiBlocking(){
        InfluxDBClient client = influxDBClient();
        return client.getWriteApiBlocking();
    }


}
